package com.example.demo.redis.repository;

import com.example.demo.model.Material;
import com.example.demo.redis.mapper.RedisZhuJieMapper;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Administrator on 2018/12/28.
 */
@Component
public class RedisZhuJieRepository {
    @Autowired
    private RedisZhuJieMapper redisZhuJieMapper;
    //查询注解redis

    @Cacheable(value = "testRedis",key = "'queryAll'")

    public Result<List<Material>> queryZhuJieMaterial() {
        System.out.println("通过数据库拿数据");
        List<Material> materials = redisZhuJieMapper.queryZhuJieMaterial();

        return ResultVo.success(materials);
    }

    //修改版注解redis
    @CacheEvict(value = "testRedis",key = "'queryAll'")
    @CachePut(value = "testRedis",key = "'update'")
    public Result<List<Material>> updateZhuJieMaterial(Material material) {
        System.out.println("通过数据库拿数据");
        redisZhuJieMapper.updateZhuJieMaterial(material);

        List<Material> materials1 = redisZhuJieMapper.queryZhuJieMaterial();
        return ResultVo.success(materials1);
    }

    @CacheEvict(value = "testRedis",key = "'delete'")
    @CachePut(value = "testRedis",key = "'update'")
    public void deleteZhuJieMaterial(Integer id) {
        redisZhuJieMapper.deleteZhuJieMaterial(id);

    }

    //添加版注解redis
    @CacheEvict(value = "testRedis",key = "'queryAll'")
    @CachePut(value = "testRedis",key = "'insert'")

    public Material insertZhuJieMaterial(Material material) {
        redisZhuJieMapper.insertZhuJieMaterial(material);
        return material;
    }
}
